package com.blrs.service.file.impl;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.EnterpriseInfo;
import com.blrs.domain.FileCabinetInformation;
import com.blrs.mapper.FileCabinetInformationMapper;
import com.blrs.service.enterprise.info.IEnterpriseInfoService;
import com.blrs.service.enterprise.info.impl.EnterpriseInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.ExternalDocumentMapper;
import com.blrs.domain.ExternalDocument;
import com.blrs.service.file.IExternalDocumentService;

/**
 * 外部文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class ExternalDocumentServiceImpl implements IExternalDocumentService 
{
    @Autowired
    private ExternalDocumentMapper externalDocumentMapper;
    @Autowired
    private FileCabinetInformationMapper fileCabinetInformationMapper;

    

    @Override
    public List<ExternalDocument> selectPermissionList(ExternalDocument externalDocument)
    {
        Long userid = SecurityUtils.getUserId();
        if(userid!=1){
            externalDocument.setBelongTo(SecurityUtils.getOrgId());
        }
        return externalDocumentMapper.selectPermissionList(externalDocument);
    }
    @Override
    public List<Map> getFileTree(FileCabinetInformation fileCabinetInformation) {
        List<FileCabinetInformation> types=externalDocumentMapper.selectFileCabinetInformationListFileTree(fileCabinetInformation);
        List<FileCabinetInformation> fList=types.stream().filter(obj->obj.getSuperiorCabinetId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<FileCabinetInformation> zList=types.stream().filter(obj->!obj.getSuperiorCabinetId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<Map> list=new ArrayList<>();
        for (FileCabinetInformation info : fList) {
            Map map=new HashMap<>();
            map.put("id",info.getCabinetId());
            map.put("idPath",info.getIdPath());
            map.put("parentId",info.getSuperiorCabinetId());//上级
            map.put("label",info.getCabinetName());
            List<Map> listMap=list(zList,info.getCabinetId());
            if(listMap.size()!=0){
                map.put("children",listMap);
            }
            list.add(map);
        }
        return list;
    }
    @Override
    public List<FileCabinetInformation> selectFileCabinetInformationListFileTree(FileCabinetInformation fileCabinetInformation)
    {
        return externalDocumentMapper.selectFileCabinetInformationListFileTree(fileCabinetInformation);
    }
    private List<Map> list(List<FileCabinetInformation> zList, String parentId){
        List<Map> list=new ArrayList<>();
        for (FileCabinetInformation info : zList) {
            if(info.getSuperiorCabinetId().equals(parentId)){
                Map map=new HashMap<>();
                map.put("id",info.getCabinetId());
                map.put("idPath",info.getIdPath());

                map.put("label",info.getCabinetName());
                List<Map> listMap=list(zList,info.getCabinetId());
                if(listMap.size()!=0){
                    map.put("children",listMap);
                }
                list.add(map);
            }
        }
        return list;
    }


    /**
     * 查询外部文件
     * 
     * @param internalId 外部文件主键
     * @return 外部文件
     */
    @Override
    public ExternalDocument selectExternalDocumentByInternalId(String internalId)
    {
        return externalDocumentMapper.selectExternalDocumentByInternalId(internalId);
    }

    /**
     * 查询外部文件列表
     * 
     * @param externalDocument 外部文件
     * @return 外部文件
     */
    @Override
    public List<ExternalDocument> selectExternalDocumentList(ExternalDocument externalDocument)
    {
        List<ExternalDocument> doc = externalDocumentMapper.selectExternalDocumentList(externalDocument);
        return doc;
    }

    /**
     * 新增外部文件
     * 
     * @param externalDocument 外部文件
     * @return 结果
     */
    @Override
    public int insertExternalDocument(ExternalDocument externalDocument)
    {
        //externalDocument.setExternalId(UUID.randomUUID().toString());
//        FileCabinetInformation data = selectFileCabinetInformationByCabinetId(externalDocument.getCabinetId());
//        externalDocument.setCabinetName(data.getCabinetName());//上级文件柜
        externalDocument.setCreationId(SecurityUtils.getUserId());
        externalDocument.setCreationName(SecurityUtils.getUsername());
        externalDocument.setCreationTime(new Timestamp(new Date().getTime()));
        externalDocument.setOrganizationId(SecurityUtils.getOrgId());
        externalDocument.setOrganizationName(SecurityUtils.getOrgName());
        externalDocument.setDepartmentId(SecurityUtils.getDeptId());
        externalDocument.setBelongTo(SecurityUtils.getOrgId());
        return externalDocumentMapper.insertExternalDocument(externalDocument);
    }

    public FileCabinetInformation selectFileCabinetInformationByCabinetId(String cabinetId)
    {
        return fileCabinetInformationMapper.selectFileCabinetInformationByCabinetId(cabinetId);
    }
    /**
     * 修改外部文件
     * 
     * @param externalDocument 外部文件
     * @return 结果
     */
    @Override
    public int updateExternalDocument(ExternalDocument externalDocument)
    {
//        FileCabinetInformation data = selectFileCabinetInformationByCabinetId(externalDocument.getCabinetId());
//        externalDocument.setCabinetName(data.getCabinetName());//上级文件柜
        externalDocument.setOperateTime(new Timestamp(new Date().getTime()));
        externalDocument.setOperateName(SecurityUtils.getUsername());
        externalDocument.setOperateId(SecurityUtils.getUserId());
        return externalDocumentMapper.updateExternalDocument(externalDocument);
    }

    /**
     * 批量删除外部文件
     * 
     * @param internalIds 需要删除的外部文件主键
     * @return 结果
     */
    @Override
    public int deleteExternalDocumentByInternalIds(String[] internalIds)
    {
        return externalDocumentMapper.deleteExternalDocumentByInternalIds(internalIds);
    }

    /**
     * 删除外部文件信息
     * 
     * @param internalId 外部文件主键
     * @return 结果
     */
    @Override
    public int deleteExternalDocumentByInternalId(String internalId)
    {
        return externalDocumentMapper.deleteExternalDocumentByInternalId(internalId);
    }
}
